<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 15-8-11
 * Time: 下午4:29
 */
interface PftRedisCacheInterface {
    public static function set(Redis $redis);
    public static function init();
    public static function setMemberAccount($mid, $account);
    public static function getMemberAccount($mid, $le);
    public static function setVerifyCode($mobile);
    public static function getVerifyCode($mobile);
    public static function rmVerifyCode($mobile);
}


class RedisCache implements PftRedisCacheInterface{
    static $redis = null;
    public static function set(Redis $redis)
    {
        self::$redis = $redis;
    }
    public static function init()
    {
        if (is_null(self::$redis)) {
            self::$redis = new Redis();
            self::$redis->connect('10.169.9.198', 6379);
//            self::$redis->connect('127.0.0.1', 6379);
//            $redis->connect('10.169.9.198', 6379);
            self::$redis->auth('myPft!12301!&');
        }
        return self::$redis;
    }

    public static function setMemberAccount($mid, $le)
    {
        $sql = "SELECT account as ac,headphoto as avatar FROM pft_member WHERE id=$mid LIMIT 1";
        if ($le instanceof \PFT\Db) {
            $stmt = $le->prepare($sql);
            $stmt->execute();
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
        } else {
            if ($_GET['cgp']) echo $sql;
            $le->query($sql);
            $row = $le->fetch_assoc();
        }
        self::init()->hMSet("minfo:$mid", $row);
        return $row;
    }

    public static function getMemberInfo($mid, $le)
    {
//        var_dump(self::init()->exists("minfo:$mid"));
        if (!self::init()->exists("minfo:$mid")) {
            $row = self::setMemberAccount($mid, $le);
        } else {
            $row = self::init()->hMGet("minfo:$mid", array('ac', 'avatar'));
        }
        return $row;
    }

    public static function getMemberAccount($mid, $le)
    {
        $row = self::getMemberInfo($mid, $le);
        return $row['ac'];
    }

    public static function setVerifyCode($mobile)
    {
        $code = genRandomString(6);
        $cache = $code . '|' . $_SERVER['REQUEST_TIME'];
//        $json = '{"c":"'.$code.'", "t":"'.$_SERVER['REQUEST_TIME'].'"}';
        self::init()->set("vc:$mobile", $cache, 300);//2分钟内有效
        return $code;
    }

    public static function getVerifyCode($mobile)
    {
        return self::init()->get("vc:$mobile");
    }

    public static function rmVerifyCode($mobile)
    {
        return self::init()->del("vc:$mobile");
    }

    public static function setWxAppId($account)
    {
        include_once '/var/www/html/new/d/module/common/Db.class.php';
        $dbConf = include '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息
        \PFT\Db::Conf($dbConf['remote_1']);
        $db = \PFT\Db::Connect();

        $sql = "SELECT appid FROM pft_wx_open WHERE account=$account LIMIT 1";
        $stmt= $db->prepare($sql);
        $stmt->execute();
        if ($appid = $stmt->fetchColumn(0)) {
            self::init()->set("wx:{$account}:appid", $appid, 7200);
            \PFT\Db::Close();
            return $appid;
        }
        return false;
    }
    public static function getWxAppId($account)
    {
        $appid = self::init()->get("wx:{$account}:appid");
        if (!$appid) {
            $appid = self::setWxAppId($account);
        }
        return $appid;
    }

    public static function close()
    {
        self::init()->close();
    }
}